package com.weetop.cfw.base.ui.activity

import android.content.Context
import android.os.Build
import android.os.Bundle
import android.view.View
import com.aries.ui.widget.progress.UIProgressDialog
import com.gyf.immersionbar.ImmersionBar
import com.kongzue.dialog.v3.TipDialog
import com.kongzue.dialog.v3.WaitDialog
import com.lzf.easyfloat.EasyFloat
import com.trello.rxlifecycle3.components.support.RxAppCompatActivity
import com.weetop.cfw.R
import com.weetop.cfw.event.DefaultEvent
import com.weetop.cfw.utils.EventBusUtils
import org.greenrobot.eventbus.Subscribe
import org.greenrobot.eventbus.ThreadMode

/**
 *@Description:基类Activity
 */
abstract class CommonBaseActivity:RxAppCompatActivity() {

    lateinit var context: Context

    lateinit var emptyView:View

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        settingBeforeSetContentView(savedInstanceState)
        val layoutId = getLayoutId()
        if(layoutId != 0){
            setContentView(layoutId)
        }
        if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP){
            ImmersionBar.with(this).transparentStatusBar().fitsSystemWindows(true,android.R.color.white).statusBarDarkFont(true).init()
        }else{
            ImmersionBar.with(this).transparentStatusBar().fitsSystemWindows(true,android.R.color.black).statusBarDarkFont(false).init()
        }

        supportActionBar?.hide()

        context = this

        initView(savedInstanceState)
        emptyView = View.inflate(this, R.layout.rv_empty_data_layout,null)
        EventBusUtils.regist(this)
        initData(savedInstanceState)

    }

    fun windowStartDark() {
        val params = window.attributes
        params.alpha = 0.6f
        window.attributes = params
    }

    fun windowStartLight() {
        val params = window.attributes
        params.alpha = 1f
        window.attributes = params
    }

    /**
     * 为控件设置单击事件
     */
    fun setViewsOnClickListener(listener: View.OnClickListener, vararg view: View){
        view.forEach {
            it.setOnClickListener(listener)
        }
    }

    @Subscribe(threadMode = ThreadMode.MAIN)
    fun defaultEvent(event: DefaultEvent) {
    }


    /**
     * 展示加载框
     */
    fun showLoadingDialog(commonBaseActivity: CommonBaseActivity, message:String){
        WaitDialog.show(commonBaseActivity,message)
    }

    /**
     * 加载框消失
     */
    fun dismissLoadingDialog(){
        WaitDialog.dismiss()
    }

    /**
     * 用于获取布局的ID
     */
    abstract fun getLayoutId():Int

    /**
     * 在setContentView方法调用之前调用的方法
     */
    open fun settingBeforeSetContentView(savedInstanceState: Bundle?){

    }

    /**
     * 初始化控件
     */
    abstract fun initView(savedInstanceState: Bundle?)

    /**
     * 设置数据的方法
     */
    abstract fun initData(savedInstanceState: Bundle?)


    override fun onDestroy() {
        EventBusUtils.unRegist(this)
        super.onDestroy()
    }
}